package bgate.hung.workerclass;

import java.util.ArrayList;
import java.util.List;

import bgate.hung.activity.ChoiseTable;
import bgate.hung.activity.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.view.View.OnTouchListener;

public class TableListAdapter extends ArrayAdapter<TableRow> {
	private Context context;
	private List<TableRow> tables = new ArrayList<TableRow>();
	private int numberTable;

	public TableListAdapter(Context context, int textViewResourceId,
			List<TableRow> objects) {
		super(context, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
		this.context = context;
		tables = objects;
	}

	public int getCount() {
		return tables.size();
	}

	public TableRow getItem(int index) {
		return tables.get(index);
	}

	public void setNumberTable(int n) {
		numberTable = n;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		final LinearLayout table1, table2, table3;
		ImageView statusTable1, statusTable2, statusTable3;
		TextView moneyTable1, moneyTable2, moneyTable3, nameTable1, nameTable2, nameTable3, maxPlayerTable1, maxPlayerTable2, maxPlayerTable3;

		if (row == null) {
			LayoutInflater inflater = (LayoutInflater) this.getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(R.layout.table_listview_item, parent, false);
		}
		table1 = (LinearLayout) row
				.findViewById(R.id.table1LinerLayout_listView_item);
		table2 = (LinearLayout) row
				.findViewById(R.id.table2LinerLayout_listView_item);
		table3 = (LinearLayout) row
				.findViewById(R.id.table3LinerLayout_listView_item);

		statusTable1 = (ImageView) row
				.findViewById(R.id.table1ImageView_listview_item);
		statusTable2 = (ImageView) row
				.findViewById(R.id.table2ImageView_listview_item);
		statusTable3 = (ImageView) row
				.findViewById(R.id.table3ImageView_listview_item);

		moneyTable1 = (TextView) row
				.findViewById(R.id.table1TextView_listview_item);
		moneyTable2 = (TextView) row
				.findViewById(R.id.table2TextView_listview_item);
		moneyTable3 = (TextView) row
				.findViewById(R.id.table3TextView_listview_item);

		nameTable1 = (TextView) row
				.findViewById(R.id.table1NameTextView_listView_item);
		nameTable2 = (TextView) row
				.findViewById(R.id.table2NameTextView_listView_item);
		nameTable3 = (TextView) row
				.findViewById(R.id.table3NameTextView_listView_item);

		maxPlayerTable1 = (TextView) row
				.findViewById(R.id.table1MaxPlayerTextView_listView_item);
		maxPlayerTable2 = (TextView) row
				.findViewById(R.id.table2MaxPlayerTextView_listView_item);
		maxPlayerTable3 = (TextView) row
				.findViewById(R.id.table3MaxPlayerTextView_listView_item);

		final TableRow tableRow = getItem(position);

		statusTable1
				.setImageBitmap(getStatusTableImage(tableRow.table1.numberPlayer));
		statusTable2
				.setImageBitmap(getStatusTableImage(tableRow.table2.numberPlayer));
		statusTable3
				.setImageBitmap(getStatusTableImage(tableRow.table3.numberPlayer));

		moneyTable1.setText(getTableMoneyString(tableRow.table1.stake));
		moneyTable2.setText(getTableMoneyString(tableRow.table2.stake));
		moneyTable3.setText(getTableMoneyString(tableRow.table3.stake));

		nameTable1.setText(Integer.toString(tableRow.table1.id + 1));
		nameTable2.setText(Integer.toString(tableRow.table2.id + 1));
		nameTable3.setText(Integer.toString(tableRow.table3.id + 1));

		maxPlayerTable1.setText(Integer.toString(tableRow.table1.maxPlayer));
		maxPlayerTable2.setText(Integer.toString(tableRow.table2.maxPlayer));
		maxPlayerTable3.setText(Integer.toString(tableRow.table3.maxPlayer));

		if (tableRow.table1.id < numberTable)
			table1.setVisibility(LinearLayout.VISIBLE);
		else
			table1.setVisibility(LinearLayout.INVISIBLE);
		if (tableRow.table2.id < numberTable)
			table2.setVisibility(LinearLayout.VISIBLE);
		else
			table2.setVisibility(LinearLayout.INVISIBLE);
		if (tableRow.table3.id < numberTable)
			table3.setVisibility(LinearLayout.VISIBLE);
		else
			table3.setVisibility(LinearLayout.INVISIBLE);

		table1.setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				int action = event.getAction();
				if (action == MotionEvent.ACTION_DOWN) {
					table1.setBackgroundResource(R.drawable.table_clicked);
				} else if (action == MotionEvent.ACTION_UP
						|| action == MotionEvent.ACTION_CANCEL) {
					table1.setBackgroundResource(R.drawable.table);
					if (action == MotionEvent.ACTION_UP)
						((ChoiseTable) context)
								.sendRORequest(tableRow.table1.id);
				}
				return true;
			}
		});

		table2.setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				int action = event.getAction();
				if (action == MotionEvent.ACTION_DOWN) {
					table2.setBackgroundResource(R.drawable.table_clicked);
				} else if (action == MotionEvent.ACTION_UP
						|| action == MotionEvent.ACTION_CANCEL) {
					table2.setBackgroundResource(R.drawable.table);
					if (action == MotionEvent.ACTION_UP)
						((ChoiseTable) context)
								.sendRORequest(tableRow.table2.id);
				}
				return true;
			}
		});

		table3.setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				int action = event.getAction();
				if (action == MotionEvent.ACTION_DOWN) {
					table3.setBackgroundResource(R.drawable.table_clicked);
				} else if (action == MotionEvent.ACTION_UP
						|| action == MotionEvent.ACTION_CANCEL) {
					table3.setBackgroundResource(R.drawable.table);
					if (action == MotionEvent.ACTION_UP)
						((ChoiseTable) context)
								.sendRORequest(tableRow.table3.id);
				}
				return true;
			}
		});

		return row;
	}

	private Bitmap getStatusTableImage(int n) {
		if (n == 0)
			return StaticBitmap.getTable0Image();
		else if (n == 1)
			return StaticBitmap.getTable1Image();
		else if (n == 2)
			return StaticBitmap.getTable2Image();
		else if (n == 3)
			return StaticBitmap.getTable3Image();
		else
			return StaticBitmap.getTable4Image();
	}

	private String getTableMoneyString(int n) {
		String s = "";
		s += Integer.toString(n) + "$";
		return s;
	}
}
